package com.shopyourstyle.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shopyourstyle.R;

public class ThemeAdapter extends BaseAdapter {
    
    private Activity activity;
    private String[] urllist;
    private String[] titlelist;
    private int[] commentnumlist;
    private int[] fitnumlist;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public ThemeAdapter(Activity a, String[] urls, String[] theme_titles, int[] commentnums, int[] fitnums) {
        activity = a;
        urllist=urls;
        commentnumlist = commentnums;
        fitnumlist =fitnums;
        titlelist = theme_titles;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return urllist.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.themeitem, null);

        TextView themetitle=(TextView)vi.findViewById(R.id.themetitle);
        
        themetitle.setText(titlelist[position]);
        
        ImageView themeimage=(ImageView)vi.findViewById(R.id.themeicon);
        themeimage.setFocusable(true);
        themeimage.setClickable(true);
        themeimage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Toast.makeText(view.getContext(), "image click", Toast.LENGTH_SHORT);
				Log.d("OnClickListener", "onClick called (themeimage)");
				Intent myIntent = new Intent(view.getContext(), SingleThemePage.class);
				view.getContext().startActivity(myIntent);

				
			}
        	
        });
        imageLoader.DisplayImage(urllist[position], activity, themeimage);

        TextView comment=(TextView)vi.findViewById(R.id.commentitem);
        TextView fit=(TextView)vi.findViewById(R.id.fititem);
        
        
        
        if(commentnumlist[position]>0){
        	comment.setClickable(true);
        	comment.setFocusable(true);
        	comment.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View view) {
    				// TODO Auto-generated method stub
    				
    				Log.d("OnClickListener", "onClick called (comment)");
    				Intent myIntent = new Intent(view.getContext(), CommentPage.class);
    				myIntent.putExtra("commentnum", commentnumlist[position]);
    				view.getContext().startActivity(myIntent);

    				
    			}
            	
            });
        	
        	
        	if(commentnumlist[position]==1){
        		comment.setText(commentnumlist[position] + " comment");
        	}
        	else{
        		comment.setText(commentnumlist[position] + " comments");
        	}
        }
        
        if(fitnumlist[position]!=0){
        	fit.setClickable(true);
        	fit.setFocusable(true);
        	fit.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View view) {
    				// TODO Auto-generated method stub
    				
    				Log.d("OnClickListener", "onClick called (fit)");
    				Intent myIntent = new Intent(view.getContext(), FitPage.class);
    				myIntent.putExtra("fitnum", fitnumlist[position]);
    				view.getContext().startActivity(myIntent);

    				
    			}
            	
            });
        	if(fitnumlist[position]>0)
            	fit.setText("fit +"+ fitnumlist[position]);
            
            else
            	fit.setText("fit "+ fitnumlist[position]);
        }
        
        
        
        return vi;
    }
}